﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using YJKArchKernel.OutWallSearch;
using YJKMaterialStatistic;

namespace YJKStatisticTable.FastStatisticTable.Utility
{
	internal class OutsideWallGrid
	{
		public OutsideWallGrid(Document doc, GridSetCommonInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_grid = grid;
			this.m_doc = doc;
			this.m_gridSet = gridSetInfo;
		}

		public bool GetStatisticGrid(List<ViewPlan> lstViewPlan)
		{
			this.m_lstViewPlan = lstViewPlan;
			this.StatisticData();
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		public bool GetStatisticGrid(List<Element> lstSelElems, Level level)
		{
			this.m_lstSelElems = lstSelElems;
			this.StatisticDataInSelArea();
			this.m_grid.Locked = false;
			return this.SetDataToGrid();
		}

		private void StatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			try
			{
				List<Wall> lstWall = this.FilterOutWall(this.m_gridSet.LinkFile);
				this.DeleteSameWall(ref lstWall);
				this.GetOutWallInfo(lstWall);
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
			}
		}

		private void DeleteSameWall(ref List<Wall> lstWall)
		{
			List<ElementId> list = new List<ElementId>();
			for (int i = 0; i < lstWall.Count; i++)
			{
				if (!list.Contains(lstWall[i].Id))
				{
					list.Add(lstWall[i].Id);
				}
				else
				{
					lstWall.Remove(lstWall[i]);
					i--;
				}
			}
		}

		private void StatisticDataInSelArea()
		{
			try
			{
				List<Wall> lstWall = new List<Wall>();
				List<Wall> list = new List<Wall>();
				ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstSelElems.Count, "统计区域中外墙的信息", ProgressBarStyle.Blocks, true, false);
				progressBarForm.Show(WindowHelper.HWndRevit);
				foreach (Element element in this.m_lstSelElems)
				{
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						return;
					}
					if (element.Category.Id.IntegerValue == -2000011)
					{
						list.Add(element as Wall);
					}
				}
				OutWallFilter.Search(this.m_doc, this.m_level.Id, list, ref lstWall, false);
				this.GetOutWallInfo(lstWall);
			}
			catch
			{
				YJKMessageBox.Show("快速统计出错");
			}
		}

		private void GetOutWallInfo(List<Wall> lstWall)
		{
			int num = 0;
			foreach (Wall wall in lstWall)
			{
				num++;
				List<string> list = new List<string>();
				if (wall.Category.Id.IntegerValue == -2003500)
				{
					IList<ElementId> stackedWallMemberIds = wall.GetStackedWallMemberIds();
					string text = "";
					if (stackedWallMemberIds != null)
					{
						foreach (ElementId elementId in stackedWallMemberIds)
						{
							Wall wall2 = this.m_doc.GetElement(elementId) as Wall;
							if (wall2 != null)
							{
								CompoundStructure compoundStructure = wall2.WallType.GetCompoundStructure();
								if (compoundStructure != null)
								{
									IList<CompoundStructureLayer> layers = compoundStructure.GetLayers();
									if (layers[0].MaterialId != ElementId.InvalidElementId)
									{
										Material material = this.m_doc.GetElement(layers[0].MaterialId) as Material;
										text = text + material.Name + "\r\n";
									}
								}
							}
						}
					}
					list.Insert(0, "");
					list.Insert(1, "外墙" + num.ToString());
					list.Insert(2, text);
					list.Insert(3, "");
					list.Insert(4, "");
				}
				else
				{
					CompoundStructure compoundStructure2 = wall.WallType.GetCompoundStructure();
					if (compoundStructure2 == null)
					{
						list.Insert(0, "");
						list.Insert(1, "外墙" + num.ToString());
						list.Insert(2, "");
						list.Insert(3, "");
						list.Insert(4, "");
					}
					else
					{
						IList<CompoundStructureLayer> layers2 = compoundStructure2.GetLayers();
						if (layers2[0].MaterialId != ElementId.InvalidElementId)
						{
							Material material2 = this.m_doc.GetElement(layers2[0].MaterialId) as Material;
							list.Insert(0, "");
							list.Insert(1, "外墙" + num.ToString());
							list.Insert(2, material2.Name);
							list.Insert(3, "");
							list.Insert(4, "");
						}
						else
						{
							list.Insert(0, "");
							list.Insert(1, "外墙" + num.ToString());
							list.Insert(2, "");
							list.Insert(3, "");
							list.Insert(4, "");
						}
					}
				}
				this.m_dicData.Add(num.ToString(), list);
			}
		}

		private bool SetDataToGrid()
		{
			if (this.m_dicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_gridSet.GridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSet.TitleFontSize;
			int num = 2;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			Dictionary<string, List<string>>.Enumerator enumerator = this.m_dicData.GetEnumerator();
			int count = this.m_dicData.Count;
			if (count == 0)
			{
				YJKMessageBox.Show("没有找到统计数据");
				return false;
			}
			this.m_grid.Rows = num + count + 1;
			foreach (KeyValuePair<string, List<string>> keyValuePair in this.m_dicData)
			{
				List<string> value = keyValuePair.Value;
				num++;
				for (int i = 1; i < value.Count; i++)
				{
					this.m_grid.Cell(num, i).Text = value[i];
					this.m_grid.Cell(num, i).Alignment = AlignmentEnum.CenterCenter;
				}
			}
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSet.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSet.FontSize;
			if (this.m_gridSet.SetColWidth)
			{
				this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			}
			else
			{
				this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = false;
			}
			for (int j = 1; j < this.m_grid.Rows; j++)
			{
				this.m_grid.Row(j).AutoFit();
			}
			if (!this.m_gridSet.SetColWidth)
			{
				for (int k = 1; k < this.m_grid.Cols; k++)
				{
					this.m_grid.Column(k).AutoFit();
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private List<Wall> FilterOutWall(bool bCalcLink)
		{
			List<Wall> list = new List<Wall>();
			List<Wall> list2 = new List<Wall>();
			List<int> list3 = new List<int>();
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstViewPlan.Count, "统计外墙的信息", ProgressBarStyle.Blocks, true, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			foreach (ViewPlan viewPlan in this.m_lstViewPlan)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					return list;
				}
				double elevation = viewPlan.GenLevel.Elevation;
				int item = (int)(elevation * 1000.0);
				if (!list3.Contains(item))
				{
					list3.Add(item);
					ElementId elementId = viewPlan.GetUnderLayParameter().AsElementId();
					Transaction transaction = new Transaction(this.m_doc, "ChangeBaseLine");
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(ElementId.InvalidElementId);
					transaction.Commit();
					list2.Clear();
					OutWallFilter.Search(this.m_doc, viewPlan.Id, ref list2, bCalcLink, true);
					if (list2.Count > 0)
					{
						list.AddRange(list2);
					}
					transaction.Start();
					viewPlan.GetUnderLayParameter().Set(elementId);
					transaction.Commit();
				}
			}
			return list;
		}

		private FlexCell.Grid m_grid;

		private Document m_doc;

		private GridSetCommonInfo m_gridSet;

		private Dictionary<string, List<string>> m_dicData = new Dictionary<string, List<string>>();

		private List<ViewPlan> m_lstViewPlan;

		private List<Element> m_lstSelElems;

		private Level m_level;
	}
}
